package com.cxytiandi.sharding.controller;

import com.cxytiandi.sharding.po.Order;
import com.cxytiandi.sharding.po.OrderItem;
import com.cxytiandi.sharding.service.OrderItemService;
import com.cxytiandi.sharding.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Random;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService itemService;

    @GetMapping("/add")
    public Object addInfo(){
        for (int i = 0; i < 100; i++) {
            Order order = new Order();
            order.setOrderNo("A000" + i);
            order.setCreateName("订单 " + i);
            order.setPrice(new BigDecimal("" +Math.random()*10+ i));
            order.setUserId(10000L+i);
            orderService.insertOrder(order);


            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getOrderId());
            orderItem.setOrderNo("A000" + i);
            orderItem.setItemName("服务项目" + i);
            orderItem.setPrice(new BigDecimal("" + i));
            itemService.insertOrderItem(orderItem);
        }
        return "success";
    }

    @GetMapping("/list")
    public Object select(@RequestParam(value = "orderId",required = false) Long orderId){
       return orderService.selectInfo(orderId);
    }

    @GetMapping("/page")
    public Object page(){
        return orderService.pageDetail();
    }
 }
